package com.clkj.xiuzheng.controller.shangcheng;

import com.clkj.xiuzheng.pojo.Product;
import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.pojo.shangcheng.OrderInfo;
import com.clkj.xiuzheng.pojo.shangcheng.ProductDiscussion;
import com.clkj.xiuzheng.pojo.shangcheng.ProductFormat;
import com.clkj.xiuzheng.pojo.shangcheng.SProduct;
import com.clkj.xiuzheng.service.shangcheng.ScProductService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/scProduct")
@CrossOrigin
@Api(value = "/scProduct", tags = {"Z商城商品管理"})
public class ScProductController {
    @Autowired
    private ScProductService scProductService;

    /*@ApiOperation(value = "根据商品类别查询商品")
    @PostMapping("/getProductByCategoryId")
    public Result getProductByCategoryId(@RequestParam Integer categoryId,
                                         @RequestParam Integer pageSize,
                                         @RequestParam Integer pageNum) {
        try {
            PageInfo pageInfo = scProductService.getProductByCategoryId(categoryId,pageSize,pageNum);
            return new Result(true, StatusCode.OK,"添加成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }*/

    @ApiOperation(value = "根据商品id查询商品详情以及商品所有规格")
    @PostMapping("/getProductById")
    public Result getProductById(@RequestParam Integer productId) {
        try {
            SProduct product = scProductService.getProductById(productId);
            return new Result(true, StatusCode.OK,"添加成功", product);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }


    @ApiOperation(value = "后台根据公司id获取商品列表")
    @PostMapping("/getProductListByCompanyId")
    public Result getProductListByCompanyId(@ApiParam(name = "companyId", value = "公司id")@RequestParam Integer companyId,
                                            @ApiParam(name = "pageSize", value = "页大小")@RequestParam Integer pageSize,
                                            @ApiParam(name = "pageNum", value = "页码")@RequestParam Integer pageNum,
                                            @ApiParam(name = "productName", value = "产品名称选填")@RequestParam(required = false) String productName,
                                            @ApiParam(name = "productStatus", value = "产品状态选填")@RequestParam(required = false) Integer productStatus,
                                            @ApiParam(name = "productKind", value = "产品类型选填")@RequestParam(required = false) Integer productKind,
                                            @ApiParam(name = "categoryOneId", value = "一级分类id选填")@RequestParam(required = false) Integer categoryOneId,
                                            @ApiParam(name = "categoryTwoId", value = "二级分类id选填")@RequestParam(required = false) Integer categoryTwoId,
                                            @ApiParam(name = "categorythreeId", value = "三级分类id选填")@RequestParam(required = false) Integer categorythreeId) {
        try {
            PageInfo pageInfo = scProductService.getProductListByCompanyId(companyId,pageSize,pageNum,productName,productStatus,productKind,categoryOneId,categoryTwoId,categorythreeId);
            return new Result(true, StatusCode.OK,"查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "批量根据商品id上下架商品 用,隔开 0下架 1上架")
    @PostMapping("/offProduct")
    public Result offProduct(@ApiParam(name = "商品ID字符串 , ", value = "productIdList")@RequestParam String productIdList,@ApiParam(name = "0下架 1上架 , ", value = "type")@RequestParam Integer type) {
        try {
            scProductService.offProduct(productIdList,type);
            return new Result(true, StatusCode.OK,"下架成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"下架失败");
        }
    }

    @ApiOperation(value = "添加商品")
    @PostMapping("/addProduct")
    public Result addProduct(SProduct product, HttpServletRequest request) {
        try {

            scProductService.addProduct(product,request);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @ApiOperation(value = "获取主页基本信息")
    @PostMapping("/getHomeInfo")
    public Result getHomeInfo(@ApiParam(name = "公司Id", value = "companyId")@RequestParam Integer companyId) {
        try {
            Map<String,Object> map = scProductService.getHomeInfo(companyId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "根据商品id查询商品统计")
    @PostMapping("/getProductCountInfo")
    public Result getProductCountInfo(@ApiParam(name = "商品id", value = "productId")@RequestParam Integer productId) {
        try {
            List<OrderInfo> list = scProductService.getProductCountInfo(productId);
            return new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("根据公司id获取所有产品名单 不分页")
    @PostMapping("/getProductListByCompanyIdNoPage")
    public Result getProductListByCompanyIdNoPage(@ApiParam(name = "公司id", value = "companyId")@RequestParam Integer companyId){
        try {
            List<SProduct> list = scProductService.getProductListByCompanyIdNoPage(companyId);
            return new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("根据商品主信息id修改商品")
    @PostMapping("/updateProduct")
    public Result updateProduct(SProduct product,HttpServletRequest request) {
        try {
            scProductService.updateProduct(product,request);
            return new Result(true, StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }
    @ApiOperation("根据规格id修改商品列表")
    @PostMapping("/updateProductByFormatId")
    public Result updateProductByFormatId(ProductFormat productFormat,HttpServletRequest request) {
        try {
             scProductService.getProductByFormatId(productFormat,request);
            return new Result(true, StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }
    @ApiOperation("根据图片id删除图片")
    @PostMapping("/deleteImage")
    public Result deleteImage(@ApiParam(name = "图片id", value = "imageId")@RequestParam Integer imageId) {
        try {
            scProductService.deleteImage(imageId);
            return new Result(true, StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"删除失败");
        }
    }
    @ApiOperation("根据商品id新增图片")
    @PostMapping("/addImage")
    public Result addProductImage(@ApiParam(name = "商品id", value = "productId") Integer productId,HttpServletRequest request) {
        try {
            scProductService.addImage(productId,request);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @ApiOperation("小程序商品详情页面")
    @PostMapping("/getDetail")
    public Result getDetail(@ApiParam(name = "商品id", value = "productId") Integer productId,Integer userId) {
        try {
            Map map = scProductService.getDetail(productId,userId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("小程序商品问答内容")
    @PostMapping("/listDiscussion")
    public Result listDiscussion(@ApiParam(name = "productId", value = "桑品ID")@RequestParam Integer productId) {
        try {
            List<ProductDiscussion> discussions = scProductService.listDiscussion(productId);
            return new Result(true, StatusCode.OK,"查询成功",discussions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("小程序首页商品搜索")
    @PostMapping("/searchProduct")
    public Result searchProduct(@ApiParam(name = "searchName", value = "搜索关键字")@RequestParam String searchName) {
        try {
            List<SProduct> productList = scProductService.searchProduct(searchName);
            return new Result(true, StatusCode.OK,"查询成功",productList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

}
